
package guipk;

import commonpk.DBOperation;
import java.sql.SQLException;


/**
 *
 * @author LJC
 */
public class LoginFrame extends javax.swing.JFrame {

    /**
     * Creates new form LoginFrame
     */
    
    //成员
    private DBOperation DBO = new DBOperation();
    private String userId;
    private String userPwd;
    private String sqlAd;
    private String sqlPlayer;
    
    //窗口
    AdGoFrame adGoFrame;
    PlayerGoFrame playerGoFrame;
    
    
    public LoginFrame() {
        initComponents();
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        identityButtonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        WelcomeLabel = new javax.swing.JLabel();
        userNameLabel = new javax.swing.JLabel();
        userPwdLabel = new javax.swing.JLabel();
        userNameTextField = new javax.swing.JTextField();
        AdRadioButton = new javax.swing.JRadioButton();
        ReaderRadioButton = new javax.swing.JRadioButton();
        loginButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        userPwdTextField = new javax.swing.JPasswordField();
        tipLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login");
        setBounds(new java.awt.Rectangle(500, 200, 0, 0));
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(0, 204, 255));
        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        WelcomeLabel.setFont(new java.awt.Font("微软雅黑", 1, 24)); // NOI18N
        WelcomeLabel.setText("Welcome");
        jPanel1.add(WelcomeLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 50, -1, -1));

        userNameLabel.setFont(new java.awt.Font("宋体", 1, 15)); // NOI18N
        userNameLabel.setText("用户名");
        jPanel1.add(userNameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 120, -1, 30));

        userPwdLabel.setFont(new java.awt.Font("宋体", 1, 15)); // NOI18N
        userPwdLabel.setText("密码");
        jPanel1.add(userPwdLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 160, -1, 30));
        jPanel1.add(userNameTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 120, 110, -1));

        AdRadioButton.setBackground(new java.awt.Color(0, 204, 255));
        identityButtonGroup.add(AdRadioButton);
        AdRadioButton.setSelected(true);
        AdRadioButton.setText("管理员");
        jPanel1.add(AdRadioButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 200, -1, -1));

        ReaderRadioButton.setBackground(new java.awt.Color(0, 204, 255));
        identityButtonGroup.add(ReaderRadioButton);
        ReaderRadioButton.setText("玩家");
        jPanel1.add(ReaderRadioButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 200, -1, -1));

        loginButton.setText("登录");
        loginButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });
        jPanel1.add(loginButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 240, 70, 40));

        exitButton.setText("退出");
        exitButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
        jPanel1.add(exitButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 240, 70, 40));
        jPanel1.add(userPwdTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 160, 110, -1));

        tipLabel.setFont(new java.awt.Font("宋体", 1, 12)); // NOI18N
        tipLabel.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(tipLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 100, 90, 20));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        // TODO add your handling code here:
        this.dispose();
        System.exit(0);
    }//GEN-LAST:event_exitButtonActionPerformed

    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginButtonActionPerformed
        // TODO add your handling code here:
        
        //Members
        boolean loginFlag = false;
        
        userId = this.userNameTextField.getText();
        userPwd = String.valueOf(this.userPwdTextField.getPassword());
        sqlAd = "select * from AdTable";
        sqlPlayer = "select * from PlayerTable";
        
        if(this.AdRadioButton.isSelected()){
            //this.adGoFrame = new AdGoFrame();
            DBO.DBQuary(sqlAd);
            try{
                while(this.DBO.getRs().next()){
                    if(userId.equals(this.DBO.getRs().getString("AdId")) && userPwd.equals(this.DBO.getRs().getString("AdPwd"))){
                        loginFlag = true;
                        break;
                    }
                }
                
                if(loginFlag){
                    adGoFrame = new AdGoFrame(userId);
                    this.dispose();
                }
                else{
                    this.tipLabel.setText("未找到用户");
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
            
        }
        else{
            
            DBO.DBQuary(sqlPlayer);
            try{
                while(DBO.getRs().next()){
                    if(userId.equals(DBO.getRs().getString("PlayerId")) && userPwd.equals(DBO.getRs().getString("PlayerPwd"))){
                        loginFlag = true;
                        break;
                    }
                }
                if(loginFlag == true){
                    this.playerGoFrame = new PlayerGoFrame(Integer.parseInt(this.userId));
                    this.dispose();
                }
                else{
                    this.tipLabel.setText("未找到用户");
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_loginButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoginFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoginFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoginFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoginFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton AdRadioButton;
    private javax.swing.JRadioButton ReaderRadioButton;
    private javax.swing.JLabel WelcomeLabel;
    private javax.swing.JButton exitButton;
    private javax.swing.ButtonGroup identityButtonGroup;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton loginButton;
    private javax.swing.JLabel tipLabel;
    private javax.swing.JLabel userNameLabel;
    private javax.swing.JTextField userNameTextField;
    private javax.swing.JLabel userPwdLabel;
    private javax.swing.JPasswordField userPwdTextField;
    // End of variables declaration//GEN-END:variables
}
